import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

class StateModelDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScopedModel (
      model: CounterModel(),
      child: Scaffold(
        appBar: AppBar(
          title: Text('StateManagementDemo'),
          elevation: 0.0,
        ),
        body: CounterWrapper(),
        floatingActionButton: ScopedModelDescendant<CounterModel>(builder: (context, _, model) => FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: model.increaseCount,
          ),
        ),
      )
    );
  }
}

class CounterWrapper extends StatelessWidget {
  // final int count;
  // final VoidCallback increaseCount;
  // const CounterWrapper(this.count,this.increaseCount,{Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
        child: ChipDemo()
      );
  }
}

class ChipDemo extends StatelessWidget {
  // final int count;
  // final VoidCallback increaseCount;
  // const ChipDemo(this.count,this.increaseCount,{Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ScopedModelDescendant<CounterModel>(builder: (context, _, model) => ActionChip(
        label: Text('${model.count}'),
        onPressed: model.increaseCount,
      )
    );
  }
}

class CounterProvider extends InheritedWidget {
  CounterProvider({Key key, this.count, this.increaseCount,this.child}) : super(key: key, child: child);

  final int count;
  final VoidCallback increaseCount; 
  final Widget child;

  static CounterProvider of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<CounterProvider>();
  }

  @override
  bool updateShouldNotify(CounterProvider oldWidget) {
    return true;
  }
}

class CounterModel extends Model {
  int _count = 0;
  int get count => _count;

  void increaseCount() {
    _count += 1;
    notifyListeners();
  }
}